package com.hhkj.admin.order.common.component.validator;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.hhkj.admin.order.common.cont.OrderConst;
import com.hhkj.admin.order.common.cont.OrderRefundConst;
import com.hhkj.admin.order.common.exception.InvalidOrderItemException;
import com.hhkj.admin.order.domain.Order;
import com.hhkj.api.sale.common.session.holder.ApiSaleSessionHolder;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;

import java.util.Date;

public class OrderValidator {

    /**
     * 订单距离支付是否已经超出24小时
     *
     * @param order 订单信息
     * @return
     */
    public static boolean isOrderOutOf24Hour(Order order) {
        Date payTime = order.getPayTime();
        DateTime dateTime = DateUtil.offsetDay(payTime, 1);
        if (dateTime.compareTo(new Date()) < 0) {
            throw new GlobalException("该订单距离支付时间已经超出24小时");
        }
        return true;
    }

    /**
     * 是否是当前商家的订单
     *
     * @param order 订单信息
     * @return
     */
    public static boolean isSelfOrder(Order order) {
        Long saleId = ApiSaleSessionHolder.getSaleId();
        if (order.getSaleId().equals(saleId) == false) {
            throw new GlobalException("该订单为他人订单");
        }
        return true;
    }

    /**
     * 是否是已支付的订单
     *
     * @param order 订单信息
     * @return
     */
    public static boolean isPaidOrder(Order order) {
        if (OrderConst.StatusEnum.PAID.getCode().equals(order.getStatusCode())) {
            return true;
        }
        throw new GlobalException("订单未支付");
    }

    /**
     * 订单是否不处于正在退款状态
     *
     * @param order 订单信息
     * @return
     */
    public static boolean isNotRefundingOrder(Order order) {
        String refundStatusCode = order.getRefundStatusCode();
        if (StringUtils.isNotBlank(refundStatusCode) && refundStatusCode.equals(OrderRefundConst.StatusEnum.REFUNDING.getCode())) {
            throw new GlobalException("该订单正在退款流程中");
        }
        return true;
    }

    /**
     * 是否是
     * @param order
     * @return
     */
    public static boolean isPendingOrder(Order order) {
        if (OrderConst.StatusEnum.PAY_AUDITING.getCode().equals(order.getStatusCode())
                || OrderConst.StatusEnum.PAYING.getCode().equals(order.getStatusCode())) {
            return true;
        }
        throw new InvalidOrderItemException("该订单不属于待支付的订单，请检查");
    }
}
